<?php

// phpMyRealty 3
//
// File Name: ajaxDropdowns.php
// File Location : ./includes/
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include xajax class
include( PATH . '/includes/xajax/xajax_core/xajax.inc.php'); 

// Start new xajax
$xajax = new xajax();

// Set the correct PATH to the xajax libraries
$xajax->configure('javascript URI', URL . '/includes/xajax/');

// ----------------------------------------------------------------------------
// fetchLocations2($arg)
//
// xajax function to fetch the second level locations depending on the first 
// level selection
//
// $arg - selected first level location
//
function fetchLocations2($arg) {

 global $db;
 global $lang;

 // Check the returned variable, set to ANY if incorrect
 if (!eregi('^[0-9]+$', $arg) && $arg != 'ANY') $arg = 'ANY';

 // Fetch the second level locations data depending on the first level selected
 $r = $db->query('SELECT catsubsel, subcategory FROM ' . LOCATION2_TABLE . ' WHERE catsel = "' . $arg . '" ORDER BY subcategory ASC');

 // If there are locations and the selection is not empty we proceed with the 
 // output
 if ($db->numrows($r) > 0 && $arg != 'ANY') {

 // Output the dropdown
 $newContent = '<br /><select name="location2" class="span3" id="location2" onchange="xajax_fetchLocations3(this.value);">';
 $newContent.= '<option value="ANY">' . $lang['Select'] . '</option>' . "\n";
   
 while ($f = $db->fetcharray($r)) {
  $newContent.= '<option value="' . $f['catsubsel'] . '">' . $f['subcategory'] . '</option>';
 }

 $newContent.= '</select>';
 
 // Instantiate the xajaxResponse object
 $objResponse = new xajaxResponse();
    
 // add a command to the response to assign the innerHTML attribute of
 // the element with id="SomeElementId" to whatever the new content is
 $objResponse->assign("location2div","innerHTML", $newContent);
 $objResponse->assign("location3div","innerHTML", "");
 
 //return the  xajaxResponse object
 return $objResponse;
  
 }
 else {
 // Instantiate the xajaxResponse object
 $objResponse = new xajaxResponse();

 // add a command to the response to assign the innerHTML attribute of
 // the element with id="SomeElementId" to whatever the new content is
 $objResponse->assign("location2div","innerHTML", "");
 $objResponse->assign("location3div","innerHTML", "");
 
 //return the  xajaxResponse object
 return $objResponse;

 }

}

// ----------------------------------------------------------------------------
// fetchLocations3($arg)
//
// xajax function to fetch the third level locations depending on the second 
// level selection
//
// $arg - selected second level location
//
function fetchLocations3($arg) {

 global $db;
 global $lang;

 // Check the returned variable, set to ANY if incorrect
 if (!eregi('^[0-9]+$', $arg) && $arg != 'ANY') $arg = 'ANY';

 // Fetch the third level locations data depending on the second level selected
 $r = $db->query('SELECT catsubsubsel, subsubcategory FROM ' . LOCATION3_TABLE . ' WHERE catsubsel = "' . $arg . '" ORDER BY subsubcategory ASC');

 // If there are locations and the selection is not empty we proceed with the 
 // output
 if ($db->numrows($r) > 0 && $arg != 'ANY') {

 // Output the dropdown
 $newContent = '<br /><select name="location3" class="span3" id="location3">';
 $newContent.= '<option value="ANY">' . $lang['Select'] . '</option>' . "\n";
    
 while ($f = $db->fetcharray($r)) {
  $newContent.= '<option value="' . $f['catsubsubsel'] . '">' . $f['subsubcategory'] . '</option>';
 }

 $newContent.= '</select>';
 
 // Instantiate the xajaxResponse object
 $objResponse = new xajaxResponse();
    
 // add a command to the response to assign the innerHTML attribute of
 // the element with id="SomeElementId" to whatever the new content is
 $objResponse->assign("location3div","innerHTML", $newContent);
 
 //return the  xajaxResponse object
 return $objResponse;
  
 }
 else {
 // Instantiate the xajaxResponse object
 $objResponse = new xajaxResponse();

 // add a command to the response to assign the innerHTML attribute of
 // the element with id="SomeElementId" to whatever the new content is
 $objResponse->assign("location3div","innerHTML", "");
 
 //return the  xajaxResponse object
 return $objResponse;

 }

}

// Register xajax functions
$xajax->registerFunction('fetchLocations2');
$xajax->registerFunction('fetchLocations3');

// Process the xajax request
$xajax->processRequest();

?>